package org.itboys.mobile.controller.web;


import org.itboys.commons.CommonConstants;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.entity.mysql.product.SundayProductCategory;
import org.itboys.mobile.service.mysql.web.SundayProductCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * 作者：jiangxiong
 * 日期：2017年5月8日
 * 联系方式 ：empty90911@126.com
 * 描述：圣代_商品_类目_controller_web
 * 
 */
@Controller
@RequestMapping("/sunday/web/product/category")
public class SundayProductCategoryController extends BaseController {

	@Autowired
	private SundayProductCategoryService categoryService;

	/**
	 * 产品系列主页
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/index")
	public String index(
			HttpServletRequest request,
			Model model){
		return "/sunday/product/category/index";
	}

	/**
	 * 获取树形产品分类
	 * @param request
	 * @param response
	 */
	@RequestMapping("/getTree")
	public void getTree(HttpServletRequest request,
			HttpServletResponse response
			
			) {
		try {
			
			List<SundayProductCategory> tree = categoryService.getCategoryTreeJson();
			AjaxUtils.renderJson(response, tree);
		} catch (Exception e) {
				e.printStackTrace();
			AjaxUtils.renderJsonporJson(response, null,
					ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
		}

	}
	/**
	 * 单个查询
	 * @param request
	 * @param response
	 */
	@RequestMapping("/findOne")
	public void findOne(HttpServletRequest request,
						HttpServletResponse response,
						@RequestParam(value="id",required=true) Long id

	) {
		try {

			SundayProductCategory category = categoryService.find(id);
			AjaxUtils.renderJson(response, category);
		} catch (Exception e) {
			e.printStackTrace();

			String msg ="";
			if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
				msg=e.getMessage();
			}else{
				msg= CommonConstants.ERROMSG;
				logger.error("/sunday/web/product/category/findOne------"+e.getMessage());
			}
			e.printStackTrace();
			AjaxUtils.renderJsonporJson(response, null,
					ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
		}

	}
	/**
	 * 新增或修改产品分类
	 * @param request
	 * @param response
	 * @param category
	 * @param imageFile
	 */
	@RequestMapping("/save")
	public void save(HttpServletRequest request,
			HttpServletResponse response,
			 @ModelAttribute SundayProductCategory category,
			 @RequestParam(value="imageFile",required=false) MultipartFile imageFile
			) {
		try {
			categoryService.save(category, imageFile);
			AjaxUtils.renderJsonporJson(response, null,
					ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));
		} catch (Exception e) {

			e.printStackTrace();
			String msg ="";
			if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
				msg=e.getMessage();
			}else{
				msg= CommonConstants.ERROMSG;
				logger.error("/sunday/web/product/category/save------"+e.getMessage());
			}
			e.printStackTrace();
			AjaxUtils.renderJsonporJson(response, null,
					ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
		}

	}



	/**
	 * 删除产品分类
	 * @param request
	 * @param response
	 * @param categoryId
	 */
	@RequestMapping("/delete")
					   public void delete(HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam(value="categoryId",required=true) Long categoryId
			
			) {
        try {

            categoryService.deleteCategory(categoryId);

			AjaxUtils.renderJsonporJson(response, null,
					ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));
        } catch (Exception e) {

            e.printStackTrace();
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/sunday/web/product/category/delete------" + e.getMessage());
            }
            e.printStackTrace();
			AjaxUtils.renderJsonporJson(response, null,
					ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }
    }
	/**
	 * 商品分类选择器
	 */
	@RequestMapping("/choose" )
	public  String choose(HttpServletRequest request,
                          @RequestParam(value="isSingle",required=true)Integer isSingle,//是否单选
                          @RequestParam(value="param",required=false)String param,//查询参数
                          @RequestParam(value="callBack",required=false)String callBack,//制定的回掉函数
                          Model model){
		model.addAttribute("param",param);
		model.addAttribute("callBack",callBack);
		if(isSingle==null){
			return "/sunday/product/category/choose/choose_erro";
		}else if(isSingle==1){
			return "/sunday/product/category/choose/choose_single";
		}else if(isSingle==0){
			return "/sunday/product/category/choose/choose_muilt";
		}

		return "/sunday/product/category/choose/choose_erro";
	}

}
